pdf文档,加源码,可选择,复制可做注释等。浅显易懂由简入深,适合初学者
2nd Edition Table of ContentsDedication.............................................................................................................................. 2What they're saying about Head First..................................................................................... 3Praise for Head First Java............................................................................................................................................................................................................... 3Praise for other Head First books co-authored by Kathy and Bert................................................................................................................................................ 3Other related books from O'Reilly.................................................................................................................................................................................................. 3Intro....................................................................................................................................... 7Who is this book for?....................................................................................................................................................................................................................... 7We know what you're thinking........................................................................................................................................................................................................ 7And we know what your brain is thinking...................................................................................................................................................................................... 7Metacognition: thinking about thinking......................................................................................................................................................................................... 7Here's what WE did:........................................................................................................................................................................................................................ 7Here's what YOU can do to bend your brain into submission........................................................................................................................................................ 7What you need for this book:.......................................................................................................................................................................................................... 7Last-minute things you need to know:............................................................................................................................................................................................ 7Technical Editors............................................................................................................................................................................................................................. 7Other people to credit:..................................................................................................................................................................................................................... 7Just when you thought there wouldn't be any more acknowledgements....................................................................................................................................... 7Chapter 1. Breaking the Surface............................................................................................ 19Section 1.1. The Way Java Works.................................................................................................................................................................................................. 20Section 1.2. What you'll do in Java................................................................................................................................................................................................ 21Section 1.3. A very brief history of Java........................................................................................................................................................................................ 22Section 1.4. Sharpen your pencil................................................................................................................................................................................................... 23Section 1.5. Sharpen your pencil answers..................................................................................................................................................................................... 24Section 1.6. Code structure in Java............................................................................................................................................................................................... 25Section 1.7. Anatomy of a class...................................................................................................................................................................................................... 26Section 1.8. Writing a class with a main........................................................................................................................................................................................ 27Section 1.9. What can you say in the main method?.................................................................................................................................................................... 28Section 1.10. Looping and looping and......................................................................................................................................................................................... 29Section 1.11. there are no Dumb Questions.................................................................................................................................................................................. 30Section 1.12. Conditional branching.............................................................................................................................................................................................. 31Section 1.13. Coding a Serious Business Application.................................................................................................................................................................... 32Section 1.14. Monday morning at Bob's........................................................................................................................................................................................ 33Section 1.15. Phrase-O-Matic........................................................................................................................................................................................................ 35Section 1.16. Fireside Chats........................................................................................................................................................................................................... 36Section 1.17. Exercise: Code Magnets........................................................................................................................................................................................... 38Section 1.18. Exercise: BE The compiler....................................................................................................................................................................................... 39Section 1.19. JavaCross 7.0........................................................................................................................................................................................................... 40Section 1.20. Mixed Messages....................................................................................................................................................................................................... 41Section 1.21. Pool Puzzle................................................................................................................................................................................................................ 42Section 1.22. Exercise Solutins: Code Magnets:........................................................................................................................................................................... 43Chapter 2. A Trip to Objectville............................................................................................. 45Section 2.1. Chair Wars................................................................................................................................................................................................................. 46Section 2.2. Brain Power................................................................................................................................................................................................................ 51Section 2.3. Making your first object............................................................................................................................................................................................ 54Section 2.4. Making and testing Movie objects............................................................................................................................................................................. 55Section 2.5. Quick! Get out of main!............................................................................................................................................................................................. 56Section 2.6. Running the Guessing Game..................................................................................................................................................................................... 58Section 2.7. Who am I?.................................................................................................................................................................................................................. 63Chapter 3. Know Your Variables........................................................................................... 67Section 3.1. Declaring a variable................................................................................................................................................................................................... 68Section 3.2. "I'd like a double mocha, no, make it an int."........................................................................................................................................................... 69Section 3.3. You really don't want to spill that............................................................................................................................................................................. 70Section 3.4. Back away from that keyword!.................................................................................................................................................................................. 71Section 3.5. This table reserved..................................................................................................................................................................................................... 71Section 3.6. Controlling your Dog object...................................................................................................................................................................................... 72Section 3.7. An object reference is just another variable value.................................................................................................................................................... 73Section 3.8. There are no Dumb Question.................................................................................................................................................................................... 74Section 3.9. Java Exposed............................................................................................................................................................................................................. 74Section 3.10. Life on the garbage-collectible heap........................................................................................................................................................................ 75Section 3.11. Life and death on the heap....................................................................................................................................................................................... 76Section 3.12. An array is like a tray of cups................................................................................................................................................................................... 77Section 3.13. Arrays are objects too............................................................................................................................................................................................... 77Section 3.14. Make an array of Dogs............................................................................................................................................................................................. 78Section 3.15. Control your Dog...................................................................................................................................................................................................... 79Section 3.16. A Dog example......................................................................................................................................................................................................... 80Section 3.17. Exercise: BE the compiler........................................................................................................................................................................................ 81Section 3.18. Exercise: Code Magnets........................................................................................................................................................................................... 82Section 3.19. Pool Puzzle............................................................................................................................................................................................................... 83Section 3.20. A Heap o' Trouble................................................................................................................................................................................................... 84Head First Java, 2nd Edition. Head First Java, 2nd Edition, ISBN: 0596009208Prepared for e.simons@icarin.fiuc.org, Eduard SimonsCopyright © 2005 Bert Bates and Kathy Sierra. This PDF is made available for personal use only during the relevant subscription term, subject to the Safari Terms of Service. Any other userequires prior written consent from the copyright owner. Unauthorized use, reproduction and/or distribution are strictly prohibited and violate applicable laws. All rights reserved.Section 3.21. Five-Minute Mystery............................................................................................................................................................................................... 85Section 3.22. Exercise Solutions: Code Magnets.......................................................................................................................................................................... 86Section 3.23. Puzzle Solutions....................................................................................................................................................................................................... 87Chapter 4. How Objects Behave............................................................................................ 89Section 4.1. Remember: a class describes what an object knows and what an object does........................................................................................................ 90Section 4.2. The size affects the bark............................................................................................................................................................................................. 91Section 4.3. You can send things to a method.............................................................................................................................................................................. 92Section 4.4. You can get things back from a method.................................................................................................................................................................... 93Section 4.5. You can send more than one thing to a method....................................................................................................................................................... 94Section 4.6. there are no Dumb Questions................................................................................................................................................................................... 96Section 4.7. Reminder: Java cares about type!............................................................................................................................................................................. 96Section 4.8. Cool things you can do with parameters and return types....................................................................................................................................... 97Section 4.9. Encapsulation............................................................................................................................................................................................................ 98Section 4.10. Encapsulating the GoodDog class......................................................................................................................................................................... 100Section 4.11. How do objects in an array behave?....................................................................................................................................................................... 101Section 4.12. Declaring and initializing instance variables........................................................................................................................................................ 102Section 4.13. The difference between instance and local variables............................................................................................................................................ 103Section 4.14. there are no Dumb Questions................................................................................................................................................................................ 103Section 4.15. Comparing variables (primitives or references).................................................................................................................................................... 104Section 4.16. Exercise: BE the compiler...................................................................................................................................................................................... 106Section 4.17. Who am I?............................................................................................................................................................................................................... 107Section 4.18. Mixed Messages..................................................................................................................................................................................................... 108Section 4.19. Pool Puzzle............................................................................................................................................................................................................. 109Section 4.20. Five Minute Mystery.............................................................................................................................................................................................. 110Section 4.21. Puzzle Solutions...................................................................................................................................................................................................... 112Chapter 5. Extra-Strength Methods..................................................................................... 113Section 5.1. Let's build a Battleship-style game: "Sink a Dot Com"............................................................................................................................................ 114Section 5.2. First, a high-level design.......................................................................................................................................................................................... 115Section 5.3. The "Simple Dot Com Game" a gentler introduction.............................................................................................................................................. 116Section 5.4. Developing a Class.................................................................................................................................................................................................... 117Section 5.5. BRAIN POWER........................................................................................................................................................................................................ 117Section 5.6. There are no Dumb Questions................................................................................................................................................................................. 121Section 5.7. There are no Dumb Questions................................................................................................................................................................................. 124Section 5.8. Exercise: BE the JVM.............................................................................................................................................................................................. 136Section 5.9. Exercise: Code Magnets........................................................................................................................................................................................... 137Section 5.10. Java Cross............................................................................................................................................................................................................... 138Section 5.11. Exercise Solutions................................................................................................................................................................................................... 140Chapter 6. Using the Java Library....................................................................................... 143Section 6.1. In our last chapter, we left you with the cliff-hanger. A bug................................................................................................................................... 144Section 6.2. So what happened?.................................................................................................................................................................................................. 145Section 6.3. How do we fix it?...................................................................................................................................................................................................... 146Section 6.4. Option one is too clunky.......................................................................................................................................................................................... 147Section 6.5. Option two is a little better, but still pretty clunky................................................................................................................................................. 147Section 6.6. Wake up and smell the library................................................................................................................................................................................. 150Section 6.7. Some things you can do with ArrayList................................................................................................................................................................... 151Section 6.8. there are no Dumb Questions.................................................................................................................................................................................. 153Section 6.9. Java Exposed............................................................................................................................................................................................................ 153Section 6.10. Comparing ArrayList to a regular array................................................................................................................................................................ 154Section 6.11. Comparing ArrayList to a regular array................................................................................................................................................................. 155Section 6.12. Let's fix the DotCom code...................................................................................................................................................................................... 156Section 6.13. New and improved DotCom class.......................................................................................................................................................................... 157Section 6.14. Let's build the REAL game: "Sink a Dot Com"...................................................................................................................................................... 158Section 6.15. What needs to change?........................................................................................................................................................................................... 159Section 6.16. Who does what in the DotComBust game (and when)......................................................................................................................................... 160Section 6.17. Prep code for the real DotComBust class............................................................................................................................................................... 162Section 6.18. The final version of the Dotcom class.................................................................................................................................................................... 168Section 6.19. Super Powerful Boolean Expressions.................................................................................................................................................................... 169Section 6.20. Ready-bake Code................................................................................................................................................................................................... 170Section 6.21. Ready-bake Code.................................................................................................................................................................................................... 171Section 6.22. Using the Library (the Java API)........................................................................................................................................................................... 172Section 6.23. You have to know the full name of the class you want to use in your code.......................................................................................................... 173Section 6.24. there are no Dumb Questions................................................................................................................................................................................ 173Section 6.25. there are no Dumb Questions................................................................................................................................................................................ 175Section 6.26. How to play with the API....................................................................................................................................................................................... 176Section 6.27. Code Magnets......................................................................................................................................................................................................... 179Section 6.28. JavaCross 7.0......................................................................................................................................................................................................... 180Section 6.29. Exercise Solutions.................................................................................................................................................................................................. 181Section 6.30. JavaCross answers................................................................................................................................................................................................. 182Chapter 7. Better Living in Objectville................................................................................. 183Section 7.1. Chair Wars Revisited................................................................................................................................................................................................ 184Section 7.2. BRAIN POWER........................................................................................................................................................................................................ 185Section 7.3. there are no Dumb Questions.................................................................................................................................................................................. 194Head First Java, 2nd Edition. Head First Java, 2nd Edition, ISBN: 0596009208Prepared for e.simons@icarin.fiuc.org, Eduard SimonsCopyright © 2005 Bert Bates and Kathy Sierra. This PDF is made available for personal use only during the relevant subscription term, subject to the Safari Terms of Service. Any other userequires prior written consent from the copyright owner. Unauthorized use, reproduction and/or distribution are strictly prohibited and violate applicable laws. All rights reserved.Section 7.4. there are no Dumb Questions.................................................................................................................................................................................. 198Section 7.5. brain power.............................................................................................................................................................................................................. 206Section 7.6. there are no Dumb Questions................................................................................................................................................................................. 207Section 7.7. Exercise: Mixed Messages....................................................................................................................................................................................... 210Section 7.8. Exercise BE the Compiler......................................................................................................................................................................................... 211Section 7.9. Exercise Solutions: BE the Compiler....................................................................................................................................................................... 213Chapter 8. Serious Polymorphism....................................................................................... 215Section 8.1. Did we forget about something when we designed this?........................................................................................................................................ 216Section 8.2. BRAIN POWER....................................................................................................................................................................................................... 220Section 8.3. there are no Dumb Questions................................................................................................................................................................................. 243Section 8.4. Pool Puzzle.............................................................................................................................................................................................................. 250Section 8.5. Exercise Solutions.................................................................................................................................................................................................... 251Chapter 9. Life and Death of an Object................................................................................ 253Section 9.1. The Stack and the Heap: where things live............................................................................................................................................................. 254Section 9.2. Methods are stacked................................................................................................................................................................................................ 255Section 9.3. What about local variables that are objects?.......................................................................................................................................................... 256Section 9.4. there are no Dumb Questions................................................................................................................................................................................. 256Section 9.5. If local variables live on the stack, where do instance variables live?.................................................................................................................... 257Section 9.6. The miracle of object creation................................................................................................................................................................................. 258Section 9.7. Construct a Duck..................................................................................................................................................................................................... 260Section 9.8. Initializing the state of a new Duck......................................................................................................................................................................... 261Section 9.9. there are no Dumb Questions.................................................................................................................................................................................. 261Section 9.10. Using the constructor to initialize important Duck state..................................................................................................................................... 262Section 9.11. Make it easy to make a Duck.................................................................................................................................................................................. 263Section 9.12. Doesn't the compiler always make a no-arg constructor for you? No!................................................................................................................. 264Section 9.13. there are no Dumb Questions................................................................................................................................................................................ 266Section 9.14. there are no Dumb Questions................................................................................................................................................................................ 267Section 9.15. Wait a minute... we never DID talk about superclasses and inheritance and how that all fits in with constructors.......................................... 268Section 9.16. Making a Hippo means making the Animal and Object parts too....................................................................................................................... 270Section 9.17. How do you invoke a superclass constructor?....................................................................................................................................................... 271Section 9.18. Can the child exist before the parents?................................................................................................................................................................. 272Section 9.19. Superclass constructors with arguments............................................................................................................................................................... 273Section 9.20. Invoking one overloaded constructor from another............................................................................................................................................ 274Section 9.21. Now we know how an object is born, but how long does an object live?............................................................................................................. 276Section 9.22. What about reference variables?........................................................................................................................................................................... 278Chapter 10. Numbers Matter............................................................................................... 291Section 10.1. MATH methods: as close as you'll ever get to a global method............................................................................................................................ 292Section 10.2. The difference between regular (non-static) and static methods......................................................................................................................... 293Section 10.3. What it means to have a class with static methods............................................................................................................................................... 294Section 10.4. Static methods can't use non-static (instance) variables!.................................................................................................................................... 295Section 10.5. Static methods can't use non-static methods, either!........................................................................................................................................... 296Section 10.6. Static variable: value is the same for ALL instances of the class.......................................................................................................................... 297Section 10.7. Initializing a static variable................................................................................................................................................................................... 299Section 10.8. static final variables are constants........................................................................................................................................................................ 300Section 10.9. final isn't just for static variables........................................................................................................................................................................... 301Section 10.10. there are no Dumb Questions............................................................................................................................................................................. 302Section 10.11. Math methods...................................................................................................................................................................................................... 304Section 10.12. Wrapping a primitive........................................................................................................................................................................................... 305Section 10.13. Before Java 5.0, YOU had to do the work........................................................................................................................................................... 306Section 10.14. Autoboxing: blurring the line bet ween primitive and object............................................................................................................................. 307Section 10.15. Autoboxing works almost everywhere................................................................................................................................................................ 308Section 10.16. But wait! There's more! Wrappers have static utility methods too!................................................................................................................... 310Section 10.17. And now in reverse... turning a primitive number into a String.......................................................................................................................... 311Section 10.18. Number formatting.............................................................................................................................................................................................. 312Section 10.19. Formatting deconstructed.................................................................................................................................................................................... 313Section 10.20. The percent (%) says, "insert argument here" (and format it using these instructions)................................................................................... 314Section 10.21. The format String uses its own little language syntax......................................................................................................................................... 315Section 10.22. The format specifier............................................................................................................................................................................................. 316Section 10.23. The only required specifier is for TYPE............................................................................................................................................................... 317Section 10.24. What happens if I have more than one argument?............................................................................................................................................. 318Section 10.25. So much for numbers, what about dates?........................................................................................................................................................... 319Section 10.26. Working with Dates............................................................................................................................................................................................. 320Section 10.27. Moving backward and for ward in time............................................................................................................................................................... 321Section 10.28. Getting an object that extends Calendar............................................................................................................................................................. 322Section 10.29. Working with Calendar objects........................................................................................................................................................................... 323Section 10.30. Highlights of the Calendar API........................................................................................................................................................................... 324Section 10.31. Even more Statics!... static imports..................................................................................................................................................................... 325Section 10.32. Lunar Code Magnets........................................................................................................................................................................................... 330Chapter 11. Risky Behavior.................................................................................................. 333Section 11.1. Let's make a Music Machine................................................................................................................................................................................... 334Section 11.2. We'll start with the basics....................................................................................................................................................................................... 335Section 11.3. First we need a Sequencer...................................................................................................................................................................................... 336Head First Java, 2nd Edition. Head First Java, 2nd Edition, ISBN: 0596009208Prepared for e.simons@icarin.fiuc.org, Eduard SimonsCopyright © 2005 Bert Bates and Kathy Sierra. This PDF is made available for personal use only during the relevant subscription term, subject to the Safari Terms of Service. Any other userequires prior written consent from the copyright owner. Unauthorized use, reproduction and/or distribution are strictly prohibited and violate applicable laws. All rights reserved.Section 11.4. Something's wrong!................................................................................................................................................................................................ 336Section 11.5. What happens when a method you want to call (probably in a class you didn't write) is risky?......................................................................... 337Section 11.6. Methods in Java use exceptions to tell the calling code, "Some thing Bad Happened. I failed."......................................................................... 338Section 11.7. The compiler needs to know that YOU know you're calling a risky method........................................................................................................ 339Section 11.8. An exception is an object… of type Exception....................................................................................................................................................... 340Section 11.9. If it's your code that catches the exception, then whose code throws it?.............................................................................................................. 341Section 11.10. there are no Dumb Questions.............................................................................................................................................................................. 342Section 11.11. Flow control in try/catch blocks........................................................................................................................................................................... 344Section 11.12. Finally: for the things you want to do no matter what........................................................................................................................................ 345Section 11.13. Sharpen your pencil:Flow Control....................................................................................................................................................................... 346Section 11.14. Did we mention that a method can throw more than one exception?................................................................................................................ 347Section 11.15. Exceptions are polymorphic................................................................................................................................................................................. 348Section 11.16. Multiple catch blocks must be ordered from smallest to biggest........................................................................................................................ 350Section 11.17. You can't put bigger baskets above smaller baskets............................................................................................................................................. 351Section 11.18. Sharpen your pencil.............................................................................................................................................................................................. 352Section 11.19. When you don't want to handle an exception…................................................................................................................................................... 353Section 11.20. Ducking (by declaring) only delays the inevitable.............................................................................................................................................. 354Section 11.21. Getting back to our music code…......................................................................................................................................................................... 356Section 11.22. Code Kitchen......................................................................................................................................................................................................... 357Section 11.23. Making actual sound............................................................................................................................................................................................ 358Section 11.24. Your very first sound player app.......................................................................................................................................................................... 360Section 11.25. Making a MidiEvent (song data).......................................................................................................................................................................... 361Section 11.26. MIDI message: the heart of a MidiEvent............................................................................................................................................................. 362Section 11.27. Anatomy of a message.......................................................................................................................................................................................... 362Section 11.28. Version 2: Using command-line args to experiment with sounds...................................................................................................................... 364Section 11.29. Exercise: True or False......................................................................................................................................................................................... 366Section 11.30. Exercise: Code Magnets....................................................................................................................................................................................... 367Section 11.31. JavaCross 7.0........................................................................................................................................................................................................ 368Section 11.32. Exercise Solutions: True or False........................................................................................................................................................................ 369Section 11.33. Code Magnets....................................................................................................................................................................................................... 369Section 11.34. JavaCross Answers............................................................................................................................................................................................... 370Chapter 12. A Very Graphic.................................................................................................. 371Section 12.1. It all starts with a window...................................................................................................................................................................................... 372Section 12.2. Your first GUI: a button on a frame...................................................................................................................................................................... 373Section 12.3. Getting a user event............................................................................................................................................................................................... 375Section 12.4. Getting a button's ActionEvent............................................................................................................................................................................. 378Section 12.5. There are no Dumb Questions.............................................................................................................................................................................. 380Section 12.6. Make your own drawing widget............................................................................................................................................................................ 382Section 12.7. Because life's too short to paint the circle a solid color when there's a gradient blend waiting for you.............................................................. 385Section 12.8. BULLET POINTS.................................................................................................................................................................................................. 386Section 12.9. We can get an event............................................................................................................................................................................................... 387Section 12.10. Let's try it with TWO buttons.............................................................................................................................................................................. 390Section 12.11. Java Exposed: This weeks interview: Instance of an Inner Class....................................................................................................................... 398Section 12.12. There are no Dumb Questions............................................................................................................................................................................. 400Section 12.13. Code Kitchen........................................................................................................................................................................................................ 404Section 12.14. Exercise: Who am I?............................................................................................................................................................................................. 412Section 12.15. Exercise: BE the compiler..................................................................................................................................................................................... 413Section 12.16. Pool Puzzle............................................................................................................................................................................................................ 414Section 12.17. Exercise Solutions: Who am I?............................................................................................................................................................................. 415Section 12.18. Pool Puzzle............................................................................................................................................................................................................ 416Chapter 13. Work on Your Swing......................................................................................... 417Section 13.1. Swing components.................................................................................................................................................................................................. 418Section 13.2. Layout Managers.................................................................................................................................................................................................... 419Section 13.3. How does the layout manager decide?.................................................................................................................................................................. 420Section 13.4. The Big Three layout managers: border, flow, and box........................................................................................................................................ 421Section 13.5. there are no Dumb Questions................................................................................................................................................................................ 430Section 13.6. Playing with Swing components............................................................................................................................................................................ 431Section 13.7. there are no Dumb Questions................................................................................................................................................................................ 434Section 13.8. Code Kitchen.......................................................................................................................................................................................................... 436Section 13.9. Making the BeatBox............................................................................................................................................................................................... 437Section 13.10. Exercise: Which code goes with which layout?................................................................................................................................................... 442Section 13.11. Code Fragments.................................................................................................................................................................................................... 443Chapter 14. Saving Objects.................................................................................................. 447Section 14.1. Capture the Beat..................................................................................................................................................................................................... 448Section 14.2. Saving State............................................................................................................................................................................................................ 449Section 14.3. Writing a serialized object to a file........................................................................................................................................................................ 450Section 14.4. Data moves in streams from one place to another................................................................................................................................................ 451Section 14.5. What really happens to an object when it's serialized?......................................................................................................................................... 452Section 14.6. But what exactly IS an object's state? What needs to be saved?........................................................................................................................... 453Section 14.7. If you want your class to be serializable, implement Serializable......................................................................................................................... 455Section 14.8. There are no Dumb Questions.............................................................................................................................................................................. 458Section 14.9. Deserialization: restoring an object...................................................................................................................................................................... 459Section 14.10. What happens during deserialization?............................................................................................................................................................... 460Head First Java, 2nd Edition. Head First Java, 2nd Edition, ISBN: 0596009208Prepared for e.simons@icarin.fiuc.org, Eduard SimonsCopyright © 2005 Bert Bates and Kathy Sierra. This PDF is made available for personal use only during the relevant subscription term, subject to the Safari Terms of Service. Any other userequires prior written consent from the copyright owner. Unauthorized use, reproduction and/or distribution are strictly prohibited and violate applicable laws. All rights reserved.Section 14.11. There are no Dumb Questions.............................................................................................................................................................................. 461Section 14.12. Saving and restoring the game characters........................................................................................................................................................... 462Section 14.13. The GameCharacter class..................................................................................................................................................................................... 463Section 14.14. Object Serialization.............................................................................................................................................................................................. 464Section 14.15. Writing a String to a Text File.............................................................................................................................................................................. 465Section 14.16. Text File Example: e-Flashcards.......................................................................................................................................................................... 466Section 14.17. Quiz Card Builder (code outline)......................................................................................................................................................................... 467Section 14.18. The java.io.File class............................................................................................................................................................................................ 470Section 14.19. The beauty of buffers............................................................................................................................................................................................ 471Section 14.20. Reading from a Text File..................................................................................................................................................................................... 472Section 14.21. Quiz Card Player (code outline)........................................................................................................................................................................... 473Section 14.22. Parsing with String split()................................................................................................................................................................................... 476Section 14.23. There are no Dumb Questions............................................................................................................................................................................. 477Section 14.24. Version ID: A Big Serialization Gotcha............................................................................................................................................................... 478Section 14.25. Using the serialVersionUID................................................................................................................................................................................. 479Section 14.26. Code Kitchen....................................................................................................................................................................................................... 480Section 14.27. Saving a BeatBox pattern..................................................................................................................................................................................... 481Section 14.28. Restoring a BeatBox pattern............................................................................................................................................................................... 482Section 14.29. Sharpen your pencil: Can they be saved?........................................................................................................................................................... 483Section 14.30. Exercise: True or False........................................................................................................................................................................................ 484Section 14.31. Code Magnets....................................................................................................................................................................................................... 485Section 14.32. Exercise Solutions............................................................................................................................................................................................... 486Chapter 15. Make a Connection........................................................................................... 488Section 15.1. Real-time Beat Box Chat........................................................................................................................................................................................ 489Section 15.2. Connecting, Sending, and Receiving..................................................................................................................................................................... 491Section 15.3. Make a network Socket connection....................................................................................................................................................................... 492Section 15.4. A TCP port is just a number: A 16-bit number that identifies a specific program on the server......................................................................... 493Section 15.5. To read data from a Socket, use a BufferedReader............................................................................................................................................... 495Section 15.6. To write data to a Socket, use a PrintWriter......................................................................................................................................................... 496Section 15.7. DailyAdviceClient code.......................................................................................................................................................................................... 498Section 15.8. Writing a simple server......................................................................................................................................................................................... 500Section 15.9. DailyAdviceServer code.......................................................................................................................................................................................... 501Section 15.10. Writing a Chat Client........................................................................................................................................................................................... 503Section 15.11. Java has multiple threads but only one Thread class.......................................................................................................................................... 507Section 15.12. What does it mean to have more than one call stack?........................................................................................................................................ 508Section 15.13. Every Thread needs a job to do: A method to put on the new thread stack........................................................................................................ 510Section 15.14. To make a job for your thread, implement the Runnable interface..................................................................................................................... 511Section 15.15. The Thread Scheduler........................................................................................................................................................................................... 514Section 15.16. there are no Dumb Questions............................................................................................................................................................................... 517Section 15.17. Putting a thread to sleep....................................................................................................................................................................................... 518Section 15.18. Using sleep to make our program more predictable........................................................................................................................................... 519Section 15.19. Making and starting two threads......................................................................................................................................................................... 520Section 15.20. What will happen?............................................................................................................................................................................................... 520Section 15.21. Um, yes. There IS a dark side. Threads can lead to concurrency 'issues'............................................................................................................ 521Section 15.22. The Ryan and Monica problem, in code.............................................................................................................................................................. 523Section 15.23. The Ryan and Monica example........................................................................................................................................................................... 524Section 15.24. We need the makeWithdrawal ( ) method to run as one atomic thing............................................................................................................... 527Section 15.25. Using an object's lock........................................................................................................................................................................................... 528Section 15.26. The dreaded "Lost Update" problem................................................................................................................................................................... 529Section 15.27. Let's run this code................................................................................................................................................................................................ 530Section 15.28. Make the increment() method atomic. Synchronize it!...................................................................................................................................... 531Section 15.29. there are no Dumb Questions.............................................................................................................................................................................. 531Section 15.30. The deadly side of synchronization..................................................................................................................................................................... 533Section 15.31. New and improved SimpleChatClient.................................................................................................................................................................. 535Section 15.32. Ready-bake Code: The really really simple Chat Server..................................................................................................................................... 537Section 15.33. there are no Dumb Questions.............................................................................................................................................................................. 539Section 15.34. Code Kitchen........................................................................................................................................................................................................ 540Section 15.35. Exercise: Code Magnets....................................................................................................................................................................................... 541Section 15.36. Exercise Solutions................................................................................................................................................................................................ 543Section 15.37. Five-Minute Mystery............................................................................................................................................................................................ 544Chapter 16. Data structures................................................................................................ 546Section 16.1. Tracking song popularity on your jukebox............................................................................................................................................................ 547Section 16.2. Here's what you have so far, without the sort:...................................................................................................................................................... 548Section 16.3. But the ArrayList class does NOT have a sort() method!..................................................................................................................................... 549Section 16.4. ArrayList is not the only collection........................................................................................................................................................................ 550Section 16.5. You could use a TreeSet... Or you could use the Collections.sort() method......................................................................................................... 551Section 16.6. Adding Collections.sort() to the Jukebox code..................................................................................................................................................... 552Section 16.7. But now you need Song objects, not just simple Strings....................................................................................................................................... 553Section 16.8. Changing the Jukebox code to use Songs instead of Strings................................................................................................................................ 554Section 16.9. It won't compile!.................................................................................................................................................................................................... 555Section 16.10. The sort() method declaration............................................................................................................................................................................. 555Section 16.11. Generics means more type-safety......................................................................................................................................................................... 557Section 16.12. Learning generics................................................................................................................................................................................................. 558Head First Java, 2nd Edition. Head First Java, 2nd Edition, ISBN: 0596009208Prepared for e.simons@icarin.fiuc.org, Eduard SimonsCopyright © 2005 Bert Bates and Kathy Sierra. This PDF is made available for personal use only during the relevant subscription term, subject to the Safari Terms of Service. Any other userequires prior written consent from the copyright owner. Unauthorized use, reproduction and/or distribution are strictly prohibited and violate applicable laws. All rights reserved.Section 16.13. Using generic CLASSES....................................................................................................................................................................................... 559Section 16.14. Using type parameters with ArrayList................................................................................................................................................................. 560Section 16.15. Using generic METHODS..................................................................................................................................................................................... 561Section 16.16. Here's where it gets weird.................................................................................................................................................................................... 562Section 16.17. Revisiting the sort() method................................................................................................................................................................................ 564Section 16.18. In generics, "extends" means "extends or implements"...................................................................................................................................... 565Section 16.19. Finally we know what's wrong............................................................................................................................................................................. 566Section 16.20. The new, improved, comparable Song class....................................................................................................................................................... 567Section 16.21. We can sort the list, but....................................................................................................................................................................................... 568Section 16.22. Using a custom comparator................................................................................................................................................................................. 569Section 16.23. Updating the Jukebox to use a Comparator........................................................................................................................................................ 570Section 16.24. Sharpen your pencil: Reverse Engineer.............................................................................................................................................................. 570Section 16.25. Sharpen your pencil: Fill-in-the-blanks.............................................................................................................................................................. 572Section 16.26. Uh-oh. The sorting all works, but now we have duplicates................................................................................................................................ 573Section 16.27. We need a Set instead of a List............................................................................................................................................................................ 574Section 16.28. The Collection API (part of it)............................................................................................................................................................................. 575Section 16.29. Using a HashSet instead of ArrayList.................................................................................................................................................................. 576Section 16.30. What makes two objects equal?........................................................................................................................................................................... 577Section 16.31. How a HashSet checks for duplicates: hashCode() and equals()........................................................................................................................ 578Section 16.32. The Song class with overridden hashCode() and equals().................................................................................................................................. 579Section 16.33. there are no Dumb Questions............................................................................................................................................................................. 580Section 16.34. And if we want the set to stay sorted, we've got TreeSet..................................................................................................................................... 581Section 16.35. What you MUST know about TreeSet................................................................................................................................................................. 582Section 16.36. TreeSet elements MUST be comparable............................................................................................................................................................. 583Section 16.37. We've seen Lists and Sets, now we'll use a Map.................................................................................................................................................. 584Section 16.38. Finally, back to generics...................................................................................................................................................................................... 585Section 16.39. Using polymorphic arguments and generics...................................................................................................................................................... 586Section 16.40. But will it work with ArrayList<Dog> ?.............................................................................................................................................................. 587Section 16.41. What could happen if it were allowed................................................................................................................................................................. 588Section 16.42. Wildcards to the rescue........................................................................................................................................................................................ 591Section 16.43. Alternate syntax for doing the same thing.......................................................................................................................................................... 592Section 16.44. there are no Dumb Questions.............................................................................................................................................................................. 592Section 16.45. Exercise: BE the compiler, advanced.................................................................................................................................................................. 593Section 16.46. Solution to the "Reverse Engineer" sharpen exercise......................................................................................................................................... 594Section 16.47. Exercise Solution.................................................................................................................................................................................................. 595Section 16.48. BE the compiler solution..................................................................................................................................................................................... 596Chapter 17. Release Your Code............................................................................................ 598Section 17.1. Deploying your application.................................................................................................................................................................................... 599Section 17.2. Imagine this scenario............................................................................................................................................................................................ 600Section 17.3. Separate source code and class files...................................................................................................................................................................... 601Section 17.4. Put your Java in a JAR.......................................................................................................................................................................................... 602Section 17.5. Running (executing) the JAR................................................................................................................................................................................ 603Section 17.6. Put your classes in packages!................................................................................................................................................................................ 604Section 17.7. Preventing package name conflicts....................................................................................................................................................................... 605Section 17.8. Compiling and running with packages.................................................................................................................................................................. 607Section 17.9. The -d flag is even cooler then we said.................................................................................................................................................................. 608Section 17.10. Making an executable JAR with packages.......................................................................................................................................................... 609Section 17.11. So where did the manifest file go?........................................................................................................................................................................ 610Section 17.12. Java Web Start...................................................................................................................................................................................................... 614Section 17.13. The .jnlp file.......................................................................................................................................................................................................... 616Section 17.14. Steps for making and deploying a Java Web Start app........................................................................................................................................ 617Section 17.15. What's First?......................................................................................................................................................................................................... 618Section 17.16. True or False......................................................................................................................................................................................................... 619Section 17.17. Summary-Cross 7.0.............................................................................................................................................................................................. 620Chapter 18. Distributed Computing.................................................................................... 624Section 18.1. Method calls are always bet ween two objects on the same heap......................................................................................................................... 625Section 18.2. What if you want to invoke a method on an object running on another machine?............................................................................................. 626Section 18.3. Object A, running on Little, wants to call a method on Object B, running on Big............................................................................................... 627Section 18.4. But you can't do that.............................................................................................................................................................................................. 627Section 18.5. The role of the 'helpers'......................................................................................................................................................................................... 629Section 18.6. Java RMI gives you the client and service helper objects!.................................................................................................................................... 631Section 18.7. How does the client get the stub object?............................................................................................................................................................... 637Section 18.8. How does the client get the stub class?................................................................................................................................................................. 638Section 18.9. Be sure each machine has the class files it needs................................................................................................................................................. 639Section 18.10. Sharpen your pencil: What's First?..................................................................................................................................................................... 640Section 18.11. Yeah, but who really uses RMI?........................................................................................................................................................................... 641Section 18.12. What about Servlets?........................................................................................................................................................................................... 642Section 18.13. A very simple Ser vlet........................................................................................................................................................................................... 644Section 18.14. HTML page with a link to this servlet................................................................................................................................................................. 644Section 18.15. There are no Dumb Questions............................................................................................................................................................................. 645Section 18.16. Just for fun, let's make the Phrase-O-Matic work as a servlet............................................................................................................................ 646Section 18.17. Phrase-O-Matic code, servlet-friendly................................................................................................................................................................. 647Section 18.18. Enterprise JavaBeans: RMI on steroids.............................................................................................................................................................. 648Head First Java, 2nd Edition. Head First Java, 2nd Edition, ISBN: 0596009208Prepared for e.simons@icarin.fiuc.org, Eduard SimonsCopyright © 2005 Bert Bates and Kathy Sierra. This PDF is made available for personal use only during the relevant subscription term, subject to the Safari Terms of Service. Any other userequires prior written consent from the copyright owner. Unauthorized use, reproduction and/or distribution are strictly prohibited and violate applicable laws. All rights reserved.Section 18.19. For our final trick... a little Jini........................................................................................................................................................................... 649Section 18.20. Adaptive discovery in action............................................................................................................................................................................... 650Section 18.21. Self-healing net work in action............................................................................................................................................................................ 652Section 18.22. Final Project: the Universal Service browser...................................................................................................................................................... 653Section 18.23. How it works:....................................................................................................................................................................................................... 654Section 18.24. The classes and interfaces:.................................................................................................................................................................................. 656Section 18.25. Sharpen your pencil............................................................................................................................................................................................. 661Section 18.26. Congratulations!.................................................................................................................................................................................................. 665Appendix A. Final Code Kitchen.......................................................................................... 666Section A.1. Final BeatBox client program.................................................................................................................................................................................. 667Appendix B. The Top Ten Topics that almost made it into the Real Book............................. 676Section B.1. #10 Bit Manipulation............................................................................................................................................................................................... 677Section B.2. #9 Immutability...................................................................................................................................................................................................... 678Section B.3. #8 Assertions........................................................................................................................................................................................................... 679Section B.4. #7 Block Scope........................................................................................................................................................................................................ 680Section B.5. #6 Linked Invocations............................................................................................................................................................................................. 681Section B.6. #5 Anonymous and Static Nested Classes............................................................................................................................................................. 682Section B.7. #5 Anonymous and Static Nested Classes, continued........................................................................................................................................... 683Section B.8. #4 Access Levels and Access Modifiers (Who Sees What).................................................................................................................................... 684Section B.9. #4 Access Levels and Access Modifiers, cont......................................................................................................................................................... 684Section B.10. #3 String and StringBuffer/StringBuilder Methods............................................................................................................................................ 686Section B.11. #2 Multidimensional Arrays.................................................................................................................................................................................. 687Section B.12. And the number one topic that didn't quite make it in... #1 Enumerations (also called Enumerated Types or Enums).................................. 688Section B.13. Using "if" and "switch" with Enums..................................................................................................................................................................... 689Section B.14. #1 Enumerations, completed................................................................................................................................................................................ 690Section B.15. Five-Minute Mystery: A Long Trip Home............................................................................................................................................................. 691Section B.16. Five-Minute Mystery Solution.............................................................................................................................................................................. 692
评论